home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 3 / Cream of the Crop 3.iso / c_lang / strpp31.zip / DEMO.CPP next >
C/C++ Source or Header  |  1994-04-18  |  18KB  |  545 lines

  1. /* -------------------------------------------------------------------- */
  2. /* String++ Version 3.10                                       04/13/94 */
  3. /*                                                                      */
  4. /* Enhanced string class for Turbo C++/Borland C++.                     */
  5. /* Copyright 1991-1994 by Carl W. Moreland                              */
  6. /*                                                                      */
  7. /* demo.cpp                                                             */
  8. /* -------------------------------------------------------------------- */
  9. /* Demonstration of String++ methods.                                   */
  10. /* -------------------------------------------------------------------- */
  11.  
  12. #include <ctype.h>
  13. #include <stdio.h>
  14. #include <conio.h>
  15. #include <stdlib.h>
  16.  
  17. #include "str.h"
  18. #include "regexp.h"
  19.  
  20. void pause(void);
  21.  
  22. void StrIntro()
  23. {
  24.   clrscr();
  25.   _setcursortype(_NOCURSOR);
  26.  
  27.   String title1 = "String++ Version 3.10";
  28.   String title2 = "Written by Carl W. Moreland";
  29.   String title3 = "Demonstration of methods & operators";
  30.   String title4 = "Hit any key to continue";
  31.   String title5 = "or <Ctrl>C to exit...";
  32.  
  33.   cout << "\n\n\n\n\n\n\n\n";
  34.   cout << justify(title1, String::CENTER, 78) << "\n";
  35.   cout << justify(title2, String::CENTER, 78) << "\n\n";
  36.   cout << justify(title3, String::CENTER, 78) << "\n\n";
  37.   cout << justify(title4, String::CENTER, 78) << "\n";
  38.   cout << justify(title5, String::CENTER, 78) << "\n";
  39.  
  40.   pause();
  41. }
  42.  
  43. void StrConstr()
  44. {
  45.   cout << "Create a String and assign it \"Hello World.\":\n";
  46.   String str1 = "Hello World.";
  47.   cout << "String str1 = \"" << str1 << "\";\n\n";
  48.  
  49.   cout << "The String contents are returned by the () operator:\n";
  50.   cout << "str1() = " << str1 << "\n\n";
  51.  
  52.   cout << "Now assign str1 to a second String:\n";
  53.   String str2 = str1;
  54.   cout << "String str2 = str1;\n";
  55.   cout << "str2() = " << str2 << "\n\n";
  56.  
  57.   cout << "We can also assign numeric values to strings:\n";
  58.   str2 = 1024;
  59.   cout << "str2 = 1024;\n";
  60.   cout << "str2() = " << str2 << "\n\n";
  61.  
  62.   cout << "Create multiple characters by passing an optional multiplier to\n";
  63.   cout << "  the String constructor:\n\n";
  64.   str2 = "/* " + String('-', 40) + " */";
  65.   cout << "str2 = \"/* \" + String(\'-\', 40) + \" */\";\n";
  66.   cout << "str2() = " << str2() << "\n\n";
  67.  
  68.   pause();
  69. }
  70.  
  71. void StrContents()
  72. {
  73.   String str1 = "Hello World.";
  74.  
  75.   cout << "Placing a number in the () operator returns the substring\n";
  76.   cout << "  of the String starting at that number:\n";
  77.   cout << "str1(6) = " << str1(6) << "\n\n";
  78.  
  79.   cout << "Placing a second number in the () operator limits the substring\n";
  80.   cout << "  to that many characters:\n";
  81.   cout << "str1(6,2) = " << str1(6,2) << "\n\n";
  82.  
  83.   cout << "The nth character is returned by the [] operator:\n";
  84.   cout << "str1[6] = " << str1[6] << "\n\n";
  85.  
  86.   cout << "The [] operator can also be used to replace the nth character:\n";
  87.   str1[6] = 'w';
  88.   cout << "str1[6] = 'w';\n";
  89.   cout << "str1() = " << str1 << "\n\n";
  90.  
  91.   cout << "Use the left(), mid(), & right() functions to return substrings:\n\n";
  92.   cout << " left(str1, 5)    = " << left(str1, 5)   << "\n";
  93.   cout << "  mid(str1, 3, 2) = " << mid(str1, 3, 2) << "\n";
  94.   cout << "right(str1, 6)    = " << right(str1, 6)  << "\n\n";
  95.  
  96.   cout << "The length of str1 is given by the Length() member function:\n";
  97.   cout << "str1.Length() = " << str1.Length() << "\n\n";
  98.  
  99.   pause();
  100. }
  101.  
  102. void StrAddOp()
  103. {
  104.   cout << "Create a new String with the contents \"only\":\n\n";
  105.   String str1 = "only";
  106.   cout << "String str1 = \"" << str1 << "\";\n\n";
  107.  
  108.   cout << "Now use the + operator to add to it:\n\n";
  109.   str1 = "This is " + str1 + " a test";
  110.   cout << "str1 = \"This is \" + str1 + \" a test\";\n";
  111.   cout << "str1() = " << str1() << "\n\n";
  112.  
  113.   cout << "Create a new String and use the += operator to append to it:\n\n";
  114.   String str2 = "Please ";
  115.   cout << "String str2 = \"" << str2 << "\";\n\n";
  116.   str2 += "stand by...";
  117.   cout << "str2 += \"stand by...\";\n";
  118.   cout << "str2() = " << str2() << "\n\n";
  119.  
  120.   cout << "Use the inserter operator to chain appendages:\n\n";
  121.   String str3;
  122.   str3 << "The value of " << 'x' << " is " << 100;
  123.   cout << "str3 \<< \"The value of \" \<< 'x' \<< \" is \" \<< 100;\n";
  124.   cout << "str3() = " << str3() << "\n\n";
  125.  
  126.   pause();
  127. }
  128.  
  129. void StrCompareOp()
  130. {
  131.   String str1 = "Hello world.";
  132.  
  133.   cout << "The == operator will work for String == String, String == char*,\n";
  134.   cout << "  or for char* == String:\n\n";
  135.   if(str1 == "Hello world.")
  136.     cout << "str1 == \"Hello world.\"\n";
  137.   else
  138.     cout << "str1 != \"Hello world.\"\n";
  139.  
  140.   if("Hello world." == str1)
  141.     cout << "\"Hello world.\" == str1\n\n";
  142.   else
  143.     cout << "\"Hello world.\" != str1\n\n";
  144.  
  145.   cout << "By default, comparisons are case sensitive:\n\n";
  146.  
  147.   if(str1 == "HELLO WORLD.")
  148.     cout << "str1 == \"HELLO WORLD.\"\n\n";
  149.   else
  150.     cout << "str1 != \"HELLO WORLD.\"\n\n";
  151.  
  152.   cout << "You can control the case sensitivity of the comparison operators\n";
  153.   cout << "  by calling the SetCaseSensitivity() function:\n\n";
  154.  
  155.   String::SetCaseSensitivity(0);
  156.   cout << "String::SetCaseSensitivity(0);\n\n";
  157.  
  158.   if(str1 == "HELLO WORLD.")
  159.     cout << "str1 == \"HELLO WORLD.\"\n\n";
  160.   else
  161.     cout << "str1 != \"HELLO WORLD.\"\n\n";
  162.  
  163.   String::SetCaseSensitivity(1);
  164.  
  165.   pause();
  166. }
  167.  
  168. void StrMiscOp()
  169. {
  170.   String str1 = "Hello world.";
  171.  
  172.   cout << "Let's create a String that's equal to str1*2:\n\n";
  173.   String str2 = str1*2;
  174.   cout << "String str2 = str1*2;\n";
  175.   cout << "str2() = " << str2() << "\n\n";
  176.  
  177.   cout << "Now multiply str2 by 2:\n\n";
  178.   str2 *= 2;
  179.   cout << "str2 *= 2;\n";
  180.   cout << "str2() = " << str2() << "\n\n";
  181.  
  182.   pause();
  183. }
  184.  
  185. void StrNumbers()
  186. {
  187.   cout << "The String constructor can also accept numbers. In the case of\n";
  188.   cout << " floating point numbers, you can also pass a format specifier\n";
  189.   cout << " which is stored and used in future conversions.\n\n";
  190.  
  191.   String str1 = 1024;
  192.   cout << "str1 = 1024;\n";
  193.   cout << "str1() = " << str1 << "\n";
  194.   str1 = 655360L;
  195.   cout << "str1 = 655360L;\n";
  196.   cout << "str1() = " << str1 << "\n\n";
  197.  
  198.   str1 = String(123.456);
  199.   cout << "str1 = String(123.456);\n";
  200.   cout << "str1() = " << str1 << "\n";
  201.   str1 = String(3.14159, "%10.4f");
  202.   cout << "str1 = String(3.14159, \"%10.4f\");\n";
  203.   cout << "str1() = " << str1 << "\n";
  204.   str1 = 123.456;
  205.   cout << "str1 = 123.456;\n";
  206.   cout << "str1() = " << str1 << "\n";
  207.   String::SetFloatFormat("%1.4e");
  208.   str1 = String(1.6e-19);
  209.   cout << "String::SetFloatFormat(\"%1.4e\");\n";
  210.   cout << "str1 = String(1.6e-19);\n";
  211.   cout << "str1() = " << str1 << "\n";
  212.   str1 = 123.456;
  213.   cout << "str1 = 123.456;\n";
  214.   cout << "str1() = " << str1 << "\n\n";
  215.  
  216.   pause();
  217. }
  218.  
  219. void StrToUpper()
  220. {
  221.   String str1 = "Hello World.";
  222.  
  223.   cout << "The C-style function toupper() will return the upper case version\n";
  224.   cout << "  of a String without changing the String itself, whereas the\n";
  225.   cout << "  member function toUpper() will convert the String internally:\n\n";
  226.  
  227.   cout << "str1() = " << str1 << "\n";
  228.   cout << "toupper(str1) = " << toupper(str1) << "\n";
  229.   cout << "str1() = " << str1 << "\n";
  230.   str1.toUpper();
  231.   cout << "str1.toUpper();\n";
  232.   cout << "str1() = " << str1 << "\n\n";
  233.  
  234.   pause();
  235. }
  236.  
  237. void StrInsDel()
  238. {
  239.   String str1 = "This is only a test";
  240.   cout << "str1() = " << str1() << "\n\n";
  241.  
  242.   cout << "The Delete() member function can be used to delete a substring:\n\n";
  243.   str1.Delete(8, 5);
  244.   cout << "str1.Delete(8, 5);\n";
  245.   cout << "str1() = " << str1() << "\n\n";
  246.  
  247.   cout << "The Insert() member function can be used to insert a substring:\n\n";
  248.   str1.Insert(8, "still ");
  249.   cout << "str1.Insert(8, \"still \");\n";
  250.   cout << "str1() = " << str1() << "\n\n";
  251.  
  252.   cout << "The Replace() member function can be used to insert a substring\n";
  253.   cout << " either by replacing another substring or by replacing a given\n";
  254.   cout << " length of characters at a position:\n\n";
  255.  
  256.   str1.Replace("is still", "continues to be");
  257.   cout << "str1.Replace(\"is still\", \"continues to be\");\n";
  258.   cout << "str1() = " << str1() << "\n\n";
  259.   str1.Replace(5, 15, "is no longer");
  260.   cout << "str1.Replace(5, 15, \"no longer\");\n";
  261.   cout << "str1() = " << str1() << "\n";
  262.  
  263.   pause();
  264. }
  265.  
  266. void StrJustify()
  267. {
  268.   cout << "The justify() function will expand a String to a total width of n\n";
  269.   cout << "  by adding blanks and justify the non-blank portion:\n\n";
  270.  
  271.   String str1 = "Hello world.";
  272.   String str1a = "│" + justify(str1, String::LEFT, 20)   + "│";
  273.   String str1b = "│" + justify(str1, String::CENTER, 20) + "│";
  274.   String str1c = "│" + justify(str1, String::RIGHT, 20)  + "│";
  275.  
  276.   cout << "String str1 = \"" << str1 << "\";\n\n";
  277.   cout << "str1a = \"│\" + justify(str1, String::LEFT, 20)   + \"│\";\n";
  278.   cout << "str1b = \"│\" + justify(str1, String::CENTER, 20) + \"│\";\n";
  279.   cout << "str1c = \"│\" + justify(str1, String::RIGHT, 20)  + \"│\";\n";
  280.   cout << "str1a() = " << str1a() << "\n";
  281.   cout << "str1b() = " << str1b() << "\n";
  282.   cout << "str1c() = " << str1c() << "\n";
  283.  
  284.   cout << "\nHere's what happens when clipping is used:\n\n";
  285.   str1a = "│" + justify(str1, String::LEFT, 8, String::CLIP)   + "│";
  286.   str1b = "│" + justify(str1, String::CENTER, 8, String::CLIP) + "│";
  287.   str1c = "│" + justify(str1, String::RIGHT, 8, String::CLIP)  + "│";
  288.  
  289.   cout << "str1a = \"│\" + justify(str1, String::LEFT, 8, String::CLIP)   + \"│\";\n";
  290.   cout << "str1b = \"│\" + justify(str1, String::CENTER, 8, String::CLIP) + \"│\";\n";
  291.   cout << "str1c = \"│\" + justify(str1, String::RIGHT, 8, String::CLIP)  + \"│\";\n";
  292.   cout << "str1a() = " << str1a() << "\n";
  293.   cout << "str1b() = " << str1b() << "\n";
  294.   cout << "str1c() = " << str1c() << "\n";
  295.  
  296.   pause();
  297. }
  298.  
  299. void StrTrim()
  300. {
  301.   cout << "The trim() function will remove leading and trailing whitespace:\n\n";
  302.   String str1 = "\t\t  Hello World. \t ";
  303.   cout << "String str1 = \"\\t\\t  Hello World. \\t \";\n\n";
  304.  
  305.   String str1a = trim(str1, String::LEFT);
  306.   cout << "str1a = trim(str1, String::LEFT);\n";
  307.   cout << "str1a() = │" << str1a() << "│\n\n";
  308.   String str1b = trim(str1, String::RIGHT);
  309.   cout << "str1b = trim(str1, String::RIGHT);\n";
  310.   cout << "str1b() = │" << str1b() << "│\n\n";
  311.   String str1c = trim(str1);
  312.   cout << "str1c = trim(str1);\n";
  313.   cout << "str1c() = │" << str1c() << "│\n\n";
  314.  
  315.   cout << "You can also specify the character to be trimmed:\n\n";
  316.   str1 = "Here we go again..........";
  317.   cout << "str1 = " << str1 << "\n";
  318.   str1.Trim(String::RIGHT, '.');
  319.   cout << "str1.Trim(String::RIGHT, '.');\n";
  320.   cout << "str1 = " << str1 << "\n";
  321.  
  322.   pause();
  323. }
  324.  
  325. void StrFind()
  326. {
  327.   int pos;
  328.   String str1 = "d:\\prog\\turboc\\tutor";
  329.  
  330.   cout << "FindFirst() returns the location of the first instance of a \n";
  331.   cout << " substring in a String. FindNext() will return the location of\n";
  332.   cout << " subsequent instances. FindLast() finds the last instance, and\n";
  333.   cout << " FindPrev() finds subsequent previous instances.\n\n";
  334.  
  335.   cout << "String str1 = \"" << str1 << "\";\n\n";
  336.  
  337.   pos = str1.FindFirst("\\");
  338.   cout << "str1.FindFirst(\"\\\") = " << pos << "\n";
  339.  
  340.   while((pos = str1.FindNext()) != -1)
  341.     cout << "str1.FindNext() = " << pos << "\n";
  342.   cout << "str1.FindNext() = " << pos << " (No more found)\n";
  343.  
  344.   pos = str1.FindLast("tu");
  345.   cout << "\nstr1.FindLast(\"tu\") = " << pos << "\n";
  346.  
  347.   while((pos = str1.FindPrev()) != -1)
  348.     cout << "str1.FindPrev() = " << pos << "\n";
  349.   cout << "str1.FindPrev() = " << pos << " (No more found)\n";
  350.  
  351.   pause();
  352. }
  353.  
  354. void Awk()
  355. {
  356.   int i, pos, num;
  357.   String *array;
  358.   String str1 = "HELLO WORLD.";
  359.   String str3 = "This is only a test";
  360.   String str4 = "Please stand by...";
  361.  
  362.   cout << "The following are AWK functions.\n\n";
  363.  
  364.   cout << "index() returns the location of a substring in a String:\n\n";
  365.   pos = index(str1, "WOR");
  366.   cout << "str1() = " << str1 << "\n";
  367.   cout << "pos = index(str1, \"WOR\");\n";
  368.   cout << "pos = " << pos << "\n";
  369.  
  370.   pause();
  371.  
  372.   cout << "split() will split a String at a given substring delimiter:\n\n";
  373.   String str7 = "d:\\prog\\turboc\\tutor";
  374.   num = split(str7, array, "\\");
  375.  
  376.   cout << "String str7 = \"d:\\prog\\turboc\\tutor\";\n";
  377.   cout << "num = split(str7, array, \"\\\");\n\n";
  378.  
  379.   cout << "In this example, str7 is split using the \\ character as the\n";
  380.   cout << "  delimiter. The results are placed in array, which now contains:\n\n";
  381.   for(i=0; i<num; i++)
  382.     cout << "array[" << i << "] = " << array[i] << "\n";
  383.  
  384.   pause();
  385.  
  386.   cout << "gsub() performs a global substitution. In this example, we want to\n";
  387.   cout << "  replace all \\'s with /'s:\n\n";
  388.   cout << "str7() = " << str7 << "\n";
  389.   i = gsub("\\", "/", str7);
  390.   cout << "i = gsub(\"\\\", \"/\", str7);\n";
  391.   cout << "str7() = " << str7 << "\n";
  392.   cout << "i = " << i << "\n";
  393.  
  394.   pause();
  395.  
  396.   cout << "sub() performs a one-time substitution:\n\n";
  397.   cout << "str1() = " << str1 << "\n";
  398.   i = sub("LO", "P ME,", str1);
  399.   cout << "i = sub(\"LO\", \"P ME,\", str1);\n";
  400.   cout << "str1() = " << str1 << "\n";
  401.   cout << "i = " << i << "\n";
  402.  
  403.   pause();
  404.  
  405.   cout << "substr() returns a substring of the String starting at n. If a\n";
  406.   cout << "  a third parameter is given, it represents the maximum number\n";
  407.   cout << "  of characters to return\n\n";
  408.  
  409.   String str_3 = substr(str3, 8);
  410.   String str_4 = substr(str4, 7, 5);
  411.   cout << "str3() = " << str3 << "\n";
  412.   cout << "substr(str3, 8) = " << str_3 << "\n\n";
  413.   cout << "str4() = " << str4 << "\n";
  414.   cout << "substr(str4, 7, 5) = " << str_4 << "\n\n";
  415.  
  416.   pause();
  417. }
  418.  
  419. void RegularExpr()
  420. {
  421.   String str1 = "void *ptr = &var;    // assign the address of var to ptr";
  422.  
  423.   cout << "Regular expressions can be used to extract comments from a file.\n";
  424.   cout << "Assume the following line of code is read into String str1:\n\n";
  425.   cout << str1 << "\n\n";
  426.   cout << "We want to find the portion that begins with \"/\/\" so create a\n";
  427.   cout << "  regular expression for this:\n\n";
  428.  
  429.   String str1a;
  430.   Regexp re1 = String("//.*");
  431.   if(match(str1, re1) != -1)
  432.     str1a = substr(str1, RSTART, RLENGTH);
  433.  
  434.   cout << "Regexp re1 = String(\"" << re1  << "\");\n";
  435.   cout << "if(match(str1, re1) != -1)\n";
  436.   cout << "  str1a = substr(str1, RSTART, RLENGTH);\n\n";
  437.   cout << "str1a() = " << str1a << "\n";
  438.  
  439.   pause();
  440.  
  441.   cout << "We can use regular expressions to test for a data type.\n\n";
  442.   Regexp reFloat  = String("^[+-]?([0-9]+[.]?[0-9]*$|[.][0-9]+$)");
  443.   Regexp reExp = String("^[+-]?([0-9]+[.]?[0-9]*|[.][0-9]+)([eE][+-]?[0-9]+)?$");
  444.   cout << "Regexp reFloat = String(\"" << reFloat  << "\");\n";
  445.   cout << "Regexp reExp = String(\"" << reExp << "\");\n\n";
  446.  
  447.   String str2 = "-1.23";
  448.   String str3 = "+1.6e-19";
  449.  
  450.   cout << "String str2 = \"" << str2 << "\";\n";
  451.   cout << "String str3 = \"" << str3 << "\";\n\n";
  452.  
  453.   cout << "if(str2 == reFloat)\n";
  454.   cout << "  cout << \"str2 is type float\\n\";\n";
  455.   cout << "if(str3 == reExp)\n";
  456.   cout << "  cout << \"str3 is exponential notation\\n\";\n\n";
  457.  
  458.   if(str2 == reFloat)
  459.     cout << "str2 is type float\n";
  460.   if(str3 == reExp)
  461.     cout << "str3 is exponential notation\n";
  462.  
  463.   pause();
  464. }
  465.  
  466. main()
  467. {
  468.   char ch;
  469.   int i;
  470.   const int n = 16;
  471.   String s[n];
  472.  
  473.   s[0]  = "String++ Version 3.1 Main Menu";
  474.   s[1]  = "(a)    Constructors       ";
  475.   s[2]  = "(b)    Contents           ";
  476.   s[3]  = "(c)    Addition           ";
  477.   s[4]  = "(d)    Comparison         ";
  478.   s[5]  = "(e)    Misc operators     ";
  479.   s[6]  = "(f)    Numbers            ";
  480.   s[7]  = "(g)    toupper            ";
  481.   s[8]  = "(h)    Insert/Delete      ";
  482.   s[9]  = "(i)    Justification      ";
  483.   s[10] = "(j)    Trim               ";
  484.   s[11] = "(k)    Find               ";
  485.   s[12] = "(l)    AWK functions      ";
  486.   s[13] = "(m)    Regular Expressions";
  487.   s[14] = "";
  488.   s[15] = "(x)    Exit               ";
  489.  
  490.   for(i=0; i<n; i++)
  491.     s[i].Justify(String::CENTER, 78, String::NOTRIM);
  492.  
  493.   StrIntro();
  494.  
  495.   while(1)
  496.   {
  497.     cout << "\n\n" << s[0] << "\n\n";
  498.     for(i=1; i<n; i++)
  499.       cout << s[i] << "\n";
  500.  
  501.     while(1)
  502.     {
  503.       ch = tolower(getch());
  504.       if(!ch)
  505.       {
  506.         getch();
  507.         continue;
  508.       }
  509.       switch(ch)
  510.       {
  511.         case 'a': clrscr(); StrConstr();    break;
  512.         case 'b': clrscr(); StrContents();  break;
  513.         case 'c': clrscr(); StrAddOp();     break;
  514.         case 'd': clrscr(); StrCompareOp(); break;
  515.         case 'e': clrscr(); StrMiscOp();    break;
  516.         case 'f': clrscr(); StrNumbers();   break;
  517.         case 'g': clrscr(); StrToUpper();   break;
  518.         case 'h': clrscr(); StrInsDel();    break;
  519.         case 'i': clrscr(); StrJustify();   break;
  520.         case 'j': clrscr(); StrTrim();      break;
  521.         case 'k': clrscr(); StrFind();      break;
  522.         case 'l': clrscr(); Awk();          break;
  523.         case 'm': clrscr(); RegularExpr();  break;
  524.         case 'x': clrscr(); _setcursortype(_NORMALCURSOR); exit(0);
  525.         default: continue;
  526.       }
  527.       break;
  528.     }
  529.   }
  530. }
  531.  
  532. void pause(void)
  533. {
  534.   char ch = getch();
  535.   if(!ch)
  536.     getch();
  537.   if(ch == 0x03)
  538.   {
  539.     _setcursortype(_NORMALCURSOR);
  540.     exit(0);
  541.   }
  542.   clrscr();
  543.   cout << "\n";
  544. }
  545.